//
//  GXlComposeViewCell.swift
//  个人微博
//
//  Created by guo3920591 on 16/4/27.
//  Copyright © 2016年 guo3920591. All rights reserved.
//

import UIKit

@objc protocol ComposeViewCellDelegte:NSObjectProtocol{
    
   optional  func  composeViewCellDidAddPicture(cell: GXlComposeViewCell)
   optional func  composeViewCellDidRemovePicture(cell: GXlComposeViewCell)
    
}

class GXlComposeViewCell: UICollectionViewCell {
    
    
    weak var delegate: ComposeViewCellDelegte?
    
    
    var image: UIImage?{
        didSet{
            
            removePictureBtn.hidden = (image == nil)
            
            if image == nil {
                
                addPictureBtn.setBackgroundImage(UIImage(named: "compose_pic_add"), forState: .Normal)
                addPictureBtn.setBackgroundImage(UIImage(named: "compose_pic_add_highlighted"), forState: .Highlighted)
                
                //清空图像按钮
                addPictureBtn.setImage(nil, forState: .Normal)
                
                return
                
            }
            
            //否则显示图像
            
          addPictureBtn.setImage(image, forState: .Normal)
            
            addPictureBtn.setBackgroundImage(nil, forState: .Normal)
            
        }
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //设置界面
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - 监听方法
    
    @objc private func clickAddpicture(){
        
        delegate?.composeViewCellDidAddPicture?(self)
        
    }
    
    @objc private func clickRemovePicture(){
       delegate?.composeViewCellDidRemovePicture?(self)
    }
    
    
    
    // MARK: - 私有控件
    
    private lazy var addPictureBtn = UIButton(gxl_title: nil, backImgName: "compose_pic_add")
    /// 删除按钮
    private lazy var removePictureBtn = UIButton(gxl_title: nil,  imgName: "compose_photo_close")
    
    
    
}
// MARK: - 设置界面
extension GXlComposeViewCell{
    
    private func setupUI(){
        
        contentView.addSubview(addPictureBtn)
        contentView.addSubview(removePictureBtn)
        
        //自动布局
     addPictureBtn.snp_makeConstraints { (make) -> Void in
        make.top.equalTo(contentView).offset(10)
        make.left.equalTo(contentView).offset(10)
        make.right.equalTo(contentView).offset(-10)
        make.bottom.equalTo(contentView).offset(-10)
        
        
        }
        
        removePictureBtn.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(contentView)
            make.right.equalTo(contentView)
            
        }
        
        addPictureBtn.addTarget(self, action: "clickAddpicture", forControlEvents: .TouchUpInside)
        
        removePictureBtn.addTarget(self, action: "clickRemovePicture", forControlEvents: .TouchUpInside)
        
        //设置内容模式
        
        addPictureBtn.imageView?.contentMode = .ScaleAspectFill
        
        
    }
    
}
